package org.millioncall.yueyoga.admin.service.impl;

import java.util.ArrayList;
import java.util.Calendar;
import java.util.Date;
import java.util.List;
import java.util.Map;

import org.apache.commons.lang.StringUtils;
import org.millioncall.openplatform.common.PaginatedList;
import org.millioncall.openplatform.util.DateTimeUtils;
import org.millioncall.yueyoga.admin.dao.CheduleDAO;
import org.millioncall.yueyoga.admin.dao.CheduleDetailsDAO;
import org.millioncall.yueyoga.admin.dao.ClazzDAO;
import org.millioncall.yueyoga.admin.dao.entity.ClassEntity;
import org.millioncall.yueyoga.admin.dao.entity.ScheduleEntity;
import org.millioncall.yueyoga.admin.model.AviatorExpression;
import org.millioncall.yueyoga.admin.model.Chedule;
import org.millioncall.yueyoga.admin.model.ClassMemberModel;
import org.millioncall.yueyoga.admin.model.ClassProduct;
import org.millioncall.yueyoga.admin.model.ClassRecord;
import org.millioncall.yueyoga.admin.model.ClassScheduleModel;
import org.millioncall.yueyoga.admin.model.Clazz;
import org.millioncall.yueyoga.admin.model.TinyClass;
import org.millioncall.yueyoga.admin.service.ClazzService;

public class ClazzServiceImpl implements ClazzService {
	private ClazzDAO clazzDAO;
	private CheduleDAO cheduleDAO;
	private CheduleDetailsDAO cheduleDetailsDAO;

	public CheduleDAO getCheduleDAO() {
		return cheduleDAO;
	}

	public void setCheduleDAO(CheduleDAO cheduleDAO) {
		this.cheduleDAO = cheduleDAO;
	}

	public ClazzDAO getClazzDAO() {
		return clazzDAO;
	}

	public void setClazzDAO(ClazzDAO clazzDAO) {
		this.clazzDAO = clazzDAO;
	}

	public CheduleDetailsDAO getCheduleDetailsDAO() {
		return cheduleDetailsDAO;
	}

	public void setCheduleDetailsDAO(CheduleDetailsDAO cheduleDetailsDAO) {
		this.cheduleDetailsDAO = cheduleDetailsDAO;
	}

	@Override
	public int deleteByPrimaryKey(Integer id) {
		return this.clazzDAO.deleteByPrimaryKey(id);
	}

	@Override
	public int insert(Clazz record) {
		return this.clazzDAO.insert(record);
	}

	@Override
	public int insertSelective(Clazz record) {
		return this.clazzDAO.insertSelective(record);
	}

	@Override
	public Clazz selectByPrimaryKey(Integer id) {
		return this.clazzDAO.selectByPrimaryKey(id);
	}

	@Override
	public int updateByPrimaryKeySelective(Clazz record) {
		return this.clazzDAO.updateByPrimaryKeySelective(record);
	}

	@Override
	public int updateByPrimaryKeyWithBLOBs(Clazz record) {
		return this.clazzDAO.updateByPrimaryKeyWithBLOBs(record);
	}

	@Override
	public int updateByPrimaryKey(Clazz record) {
		return this.clazzDAO.updateByPrimaryKey(record);
	}

	@Override
	public PaginatedList<ClassRecord> findClazzPage(Integer start, Integer limit,
			Map<String, Object> map) {
		return this.clazzDAO.findClazzPage(start, limit, map);
	}

	@Override
	public Clazz findById(Map<String, Object> map) {
		return this.clazzDAO.findById(map);
	}

	@Override
	public int deleteClazzByIds(List<Integer> Ids) {
		return this.clazzDAO.deleteClazzByIds(Ids);
	}

	@Override
	public String findClassCode() {
		return this.clazzDAO.findClassCode();
	}

	@Override
	public boolean saveClassSaveEntity(ClassEntity entity) {
		boolean b = this.clazzDAO.saveClassEntity(entity);
		Integer newClassId = entity.getId();
		if (b) {
			if (entity.getSchedules() != null) {
				for (ScheduleEntity e : entity.getSchedules()) {
					e.setClassId(newClassId);
					e.setState("未开启预定");
					boolean b1 = this.cheduleDAO.saveScheduleEntity(e);
					if (!b1) {
						throw new RuntimeException("保存课程信息失败");
					}
				}
			}
		}
		return b;
	}

	@Override
	public boolean updateClassEntity(ClassEntity entity) {
		boolean b = this.clazzDAO.updateClassEntity(entity);
		if (b) {
			/*
			 * this.cheduleDAO .deleteScheduleByClassIds(Arrays.asList(new
			 * Integer[] { entity.getId() }));
			 */
			// 再重新保存
			if (entity.getSchedules() != null && !entity.getSchedules().isEmpty()) {
				for (ScheduleEntity e : entity.getSchedules()) {
					boolean b1 = this.cheduleDAO.saveScheduleEntity(e);
					if (!b1) {
						throw new RuntimeException("保存课程信息失败");
					}
				}
			}
		}
		return b;
	}

	@Override
	public ClassEntity findByClassId(Map<String, Object> map) {
		ClassEntity rtn = this.clazzDAO.findByClassId(map);
		List<ScheduleEntity> schedules = this.cheduleDAO.findByClassId(map);
		if (schedules == null) {
			schedules = new ArrayList<ScheduleEntity>();
		}
		rtn.setSchedules(schedules);
		return rtn;
	}

	@Override
	public boolean addMemberToClass(Integer classId, Integer memberId, String createdBy) {
		return this.clazzDAO.addMemberToClass(classId, memberId, createdBy);
	}

	@Override
	public boolean addProductToClass(Integer classId, Integer productId, String productName,
			String createdBy) {
		return this.clazzDAO.addProductToClass(classId, productId, productName, createdBy);
	}

	@Override
	public boolean containsMember(Integer classId, Integer memberId) {
		return this.clazzDAO.countOfMember(classId, memberId) > 0;
	}

	@Override
	public PaginatedList<ClassMemberModel> findClassMembersByClassId(Map<String, Object> map,
			int start, int limit) {
		return this.clazzDAO.findClassMembersByClassId(map, start, limit);
	}

	@Override
	public boolean deleteMemberOfClass(Integer classId, List<Integer> memberIds) {
		return this.clazzDAO.deleteMemberOfClass(classId, memberIds);
	}

	@Override
	public boolean deleteProductOfClass(Integer classId, List<Integer> productIds) {
		return this.clazzDAO.deleteProductOfClass(classId, productIds);
	}

	@Override
	public List<ClassProduct> findClassProduct(Map<String, Object> map) {
		return this.clazzDAO.findClassProduct(map);
	}

	@Override
	public boolean containsProduct(Integer classId, Integer productId) {
		return this.clazzDAO.countOfProduct(classId, productId) > 0;
	}

	@Override
	public List<TinyClass> findAllClassTiny(Map<String, Object> map) {
		return this.clazzDAO.findAllClassTiny(map);
	}

	@Override
	public List<Chedule> findScheduleExportPage(Map<String, Object> map) {
		return this.cheduleDAO.getCheduleInPage(0, Integer.MAX_VALUE, map);
	}

	/**
	 * 获取班级课程信息列表
	 */
	@Override
	public List<ClassScheduleModel> findClassScheduleList(Date curDate, String clsId) {
		DateTimeUtils dtu = DateTimeUtils.getInstance();
		// --获取当前日期实例
		Calendar c = Calendar.getInstance();
		// -- 组中集合数据
		List<ClassScheduleModel> modelList = new ArrayList<ClassScheduleModel>();
		ClassScheduleModel model = null;
		if (StringUtils.isBlank(clsId)) {
			return null;
		}
		Integer classId = Integer.valueOf(clsId);
		// 循环遍历7天的课程
		for (int i = 0; i < 7; i++) {
			model = new ClassScheduleModel();
			c.setTime(curDate);
			c.add(Calendar.DAY_OF_MONTH, i);
			Integer todayWeek = c.get(Calendar.DAY_OF_WEEK) - 1;
			// --设置课程日期
			model.setKcrq(dtu.format(c.getTime(), "yyyy-MM-dd"));
			model.setSfyx("0");
			// --若为0，则说明为周末，又因数据库存储的是7，则作一下转换
			if (0 == todayWeek) {
				todayWeek = 7;
			}
			// --设置课程星期
			model.setKcxq(getKcxq(todayWeek));
			model.setBjbh(clsId);
			String date = DateTimeUtils.getInstance().format(c.getTime(), "yyyy-MM-dd");
			// --获取旧课程表数据
			List<ClassScheduleModel> models = cheduleDAO.findClassSchedulesByClsId(classId,
					todayWeek, date);
			// --从原始课程表取出数据
			if (models != null && !models.isEmpty()) {
				for (int j = 0; j < models.size(); j++) {
					// --获取原始课程判断课程
					ClassScheduleModel oldModel = models.get(j);
					if (oldModel == null) {
						continue;
					}
					String ksjs = oldModel.getKssj() + " ~ " + oldModel.getJssj() + "#";
					String str = "#";
					if (j == 0) {
						model.setKsjs(ksjs);
						model.setKcxh(oldModel.getKcxh() + str);
						model.setKcbh(oldModel.getKcbh() + str);
						model.setKcls(oldModel.getKcls() + str);
						model.setKczj(oldModel.getKczj() + str);
						model.setKccd(oldModel.getKccd() + str);
						model.setSfyx("0#");

						model.setKcmc(oldModel.getKcmc() + str);
						model.setZxws(oldModel.getZxws() + str);
					} else {
						model.setKsjs(model.getKsjs() + ksjs);
						model.setKcxh(model.getKcxh() + oldModel.getKcxh() + str);
						model.setKcbh(model.getKcbh() + oldModel.getKcbh() + str);
						model.setKcls(model.getKcls() + oldModel.getKcls() + str);
						model.setKczj(model.getKczj() + oldModel.getKczj() + str);
						model.setKccd(model.getKccd() + oldModel.getKccd() + str);
						String sfyx = "0#";

						model.setKcmc(model.getKcmc() + oldModel.getKcmc() + str);
						model.setZxws(model.getZxws() + oldModel.getZxws() + str);
						if (StringUtils.isNotBlank(oldModel.getSfyx())) {
							sfyx = oldModel.getSfyx();
						}
						model.setSfyx(model.getSfyx() + sfyx);
					}
				}
			}

			// --获取新课程表数据
			List<ClassScheduleModel> newScheduleList = cheduleDetailsDAO.findClsScheduleByClsId(
					classId, date);
			// --获取新课程表中的数据
			if (newScheduleList != null && !newScheduleList.isEmpty()) {
				for (int k = 0; k < newScheduleList.size(); k++) {
					ClassScheduleModel newModel = newScheduleList.get(k);
					if (newModel == null || "3".equals(newModel.getSfyx())) {
						continue;
					}
					String ksjs = newModel.getKssj() + " ~ " + newModel.getJssj() + "#";
					String str = "#";
					if (k == 0 && (StringUtils.isBlank(model.getKsjs()))) {
						model.setKsjs(ksjs);
						model.setKcxh(newModel.getKcxh() + str);
						model.setKcbh(newModel.getKcbh() + str);
						model.setKcls(newModel.getKcls() + str);
						model.setKczj(newModel.getKczj() + str);
						model.setKccd(newModel.getKccd() + str);
						model.setSfyx(newModel.getSfyx() + str);

						model.setKcmc(newModel.getKcmc() + str);
						model.setZxws(newModel.getZxws() + str);
					} else {
						model.setKsjs(model.getKsjs() + ksjs);
						model.setKcxh(model.getKcxh() + newModel.getKcxh() + str);
						model.setKcbh(model.getKcbh() + newModel.getKcbh() + str);
						model.setKcls(model.getKcls() + newModel.getKcls() + str);
						model.setKczj(model.getKczj() + newModel.getKczj() + str);
						model.setKccd(model.getKccd() + newModel.getKccd() + str);
						String sfyx = "0#";

						model.setKcmc(model.getKcmc() + newModel.getKcmc() + str);
						model.setZxws(model.getZxws() + newModel.getZxws() + str);

						if (StringUtils.isNotBlank(newModel.getSfyx())) {
							sfyx = newModel.getSfyx() + str;
						}
						model.setSfyx(model.getSfyx() + sfyx);
					}
				}
			}
			modelList.add(model);
		}
		return modelList;
	}

	public String getKcxq(Integer xq) {
		String str = "";
		switch (xq) {
		case 1:
			str = "一";
			break;
		case 2:
			str = "二";
			break;
		case 3:
			str = "三";
			break;
		case 4:
			str = "四";
			break;
		case 5:
			str = "五";
			break;
		case 6:
			str = "六";
			break;
		case 7:
			str = "日";
			break;
		default:
			break;
		}
		return str;
	}

	@Override
	public List<ClassRecord> getAllClazzWithAviator(Map<String, Object> params,
			AviatorExpression expression, Map<String, Object> aviatorParams, int limit) {
		return this.clazzDAO.getAllClazzWithAviator(params, expression, aviatorParams, limit);
	}
}
